package com.MoveEat.activities;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.MoveEat.R;
import com.MoveEat.Utils.Consts;
import com.MoveEat.Utils.Utils;

public class SearchActivity extends ActivityWrapper
{

	private RadioGroup	radioGroup;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_layout);
		Button searchBtn = (Button) findViewById(R.id.search_btn);

		radioGroup = (RadioGroup) findViewById(R.id.radio_group);
		final EditText restText = (EditText) findViewById(R.id.rest_text);
		restText.setOnEditorActionListener(new OnEditorActionListener()
		{
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
			{
				if (actionId == EditorInfo.IME_ACTION_SEARCH)
				{
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(restText.getWindowToken(), 0);
					searchByRestName(restText.getText().toString());
					return true;
				}
				return false;
			}
		});

		final EditText streetText = (EditText) findViewById(R.id.street_text);
		streetText.setOnEditorActionListener(new OnEditorActionListener()
		{

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
			{
				if (actionId == EditorInfo.IME_ACTION_SEARCH)
				{
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(streetText.getWindowToken(), 0);
					searchByStreet(streetText.getText().toString());
					return true;
				}
				return false;
			}
		});
		searchBtn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (radioGroup.getCheckedRadioButtonId() == R.id.rest_rad_btn)
				{
					searchByRestName(restText.getText().toString());
				}
				else
				{
				}

			}
		});

	}

	protected void searchByRestName(final String string)
	{
		new AsyncTask<Void, String, String>()
		{
			@Override
			protected void onPreExecute()
			{
				SearchActivity.this.showLoading(this);
			}

			@Override
			protected String doInBackground(Void... arg0)
			{
				getAllCategories();
				return (Utils.preformRequest(Utils.BASE_URL + "GetRestDetails.php?search_name=" + string));
			}

			@Override
			protected void onPostExecute(String body)
			{
				SearchActivity.this.removeDialog(ProgressDialog.STYLE_HORIZONTAL);
				JSONArray arr = null;
				try
				{
					JSONObject obj = new JSONObject(body);
					arr = obj.optJSONArray("restaurants");
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}

				if (arr != null && arr.length() > 0)
				{
					Intent intent = new Intent(getBaseContext(), LocationActivity.class);
					intent.putExtra(Consts.Extras.SEARCH_RESULTS, body);
					intent.putExtra(Consts.Extras.CATS, mCatHashMap);
					startActivity(intent);
				}
				else
				{
					Builder build = new AlertDialog.Builder(SearchActivity.this);
					build.setMessage(R.string.no_restaurants_were_found);
					AlertDialog dialog = build.create();
					dialog.show();
				}
			}

		}.execute();

	}

	protected void searchByStreet(final String string)
	{
		new AsyncTask<Void, String, String>()
		{
			@Override
			protected void onPreExecute()
			{
				SearchActivity.this.showLoading(this);
			}

			@Override
			protected String doInBackground(Void... arg0)
			{
				getAllCategories();
				return (Utils.preformRequest(Utils.BASE_URL + "GetRestDetails.php?address=" + URLEncoder.encode(string)));
			}

			@Override
			protected void onPostExecute(String body)
			{
				if (body != null)
				{
					Intent intent = new Intent(getBaseContext(), LocationActivity.class);
					intent.putExtra(Consts.Extras.SEARCH_RESULTS, body);
					intent.putExtra(Consts.Extras.CATS, mCatHashMap);
					startActivity(intent);
				}
			}

		}.execute();

	}

}
